package com.dingup.paike.controller.statistics.classHour;

import com.dingup.api.Util.JwtTokenUtil;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.page.PageData;
import com.dingup.paike.adapter.pk.PkLessonUserAdapter;
import com.dingup.paike.enums.PkLessonUserTypeEnum;
import com.dingup.paike.service.pk.StatisticsService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/11/1.
 */
@Controller
@RequestMapping("/statistics/classHour")
public class ClassHourController {

    @Autowired
    StatisticsService statisticsService;

    @RequestMapping(value = "/get_student_class_hour.do", method = RequestMethod.POST)
    public void getStudentClassHour(HttpServletRequest request,
                                    HttpServletResponse response,
                                    String dateTime,
                                    String studentName,
                                    Integer page,
                                    Integer rows) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取学生的课时课消统计");
        try {
            String userAccount = JwtTokenUtil.getUserAccountFromRequset(request);
            PageData res = statisticsService.getStudentClassHourStatistics(dateTime, studentName, userAccount, page, rows);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_teacher_class_hour.do", method = RequestMethod.POST)
    public void getTeacherClassHour(HttpServletRequest request,
                                    HttpServletResponse response,
                                    String dateTime,
                                    String teacherName,
                                    Integer page,
                                    Integer rows) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取老师的课时课消统计");
        try {
            PageData res = statisticsService.userClassHourStatistics(dateTime, teacherName, page, rows);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_user_details.do", method = RequestMethod.POST)
    public void getStudentDetails(HttpServletRequest request,
                                  HttpServletResponse response,
                                  String userId,
                                  Integer classId,
                                  String dateTime,
                                  PkLessonUserTypeEnum type) {
        HttpResContent<List<PkLessonUserAdapter>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取用户的课时课消统计-详情");
        try {
            if (classId == null) {
                throw new Exception("classId不能为空");
            }
            List<PkLessonUserAdapter> res = statisticsService.getStatisticsClassHourDetails(userId, classId, dateTime, type);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_teacher_details.do", method = RequestMethod.POST)
    public void getTeacherDetails(HttpServletRequest request,
                                  HttpServletResponse response,
                                  String userId,
                                  Integer lessonId,
                                  String dateTime) {
        HttpResContent<List<PkLessonUserAdapter>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取老师的课时课消统计-详情");
        try {
            if (lessonId == null) {
                throw new Exception("lessonId不能为空");
            }
            List<PkLessonUserAdapter> res = statisticsService.getStatisticsClassHourDetailsForTeacher(userId, lessonId, dateTime);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
